package iamutkarshtiwari.github.io.ananas.editimage.adapter;

import iamutkarshtiwari.github.io.ananas.ResourceTable;
import iamutkarshtiwari.github.io.ananas.editimage.utils.LoadingView;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 *  FilterAdapter
 */
public class FilterAdapter extends BaseItemProvider {

    public Component roundProgress;
    public LoadingView loadingView;

    public static class Item {
         String filter;
         String filtersImage;

        public Item(String filter, String filtersImage) {
            this.filter = filter;
            this.filtersImage = filtersImage;
        }
    }

    private Context context;
    private List<Item> itemList;
    //    private FilterListFragment fragment;
    List<Integer> images;

    /**
     * 构造
     *
     * @param context   Context对象
     * @param container ComponentContainer对象
     * @param itemList  item列表
     */
    public FilterAdapter(Context context, ComponentContainer container, List<Item> itemList) {
        this.context = context;
//        this.fragment = (FilterListFragment) container;
        this.itemList = itemList;

        images = new ArrayList<>();
        images.add(ResourceTable.Media_filter_1);
        images.add(ResourceTable.Media_filter_2);
        images.add(ResourceTable.Media_filter_3);
        images.add(ResourceTable.Media_filter_4);
        images.add(ResourceTable.Media_filter_5);
        images.add(ResourceTable.Media_filter_6);
        images.add(ResourceTable.Media_filter_7);
        images.add(ResourceTable.Media_filter_8);
        images.add(ResourceTable.Media_filter_9);
        images.add(ResourceTable.Media_filter_10);
        images.add(ResourceTable.Media_filter_11);
        images.add(ResourceTable.Media_filter_12);

        initDialog();
    }

    private void initDialog() {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_view_loading, null, true);
        roundProgress = component.findComponentById(ResourceTable.Id_roundprogressbar);
        loadingView = LoadingView.decorate(context, component)
                .setSize(900, 230)
                .build();
    }


    @Override
    public int getCount() {
        return itemList == null ? 0 : itemList.size();
    }

    @Override
    public Object getItem(int position) {
        if (itemList != null && position >= 0 && position < itemList.size()) {
            return itemList.get(position);
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_filter_item, null, false);
        } else {
            cpt = component;
        }

        Text text = (Text) cpt.findComponentById(ResourceTable.Id_filter_name);
        text.setText(itemList.get(position).filter);

        Image image = (Image) cpt.findComponentById(ResourceTable.Id_filter_image);
        image.setPixelMap(images.get(position));
        return cpt;
    }

}
